import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login/index'
import Home from '@/views/Home/index'
import newsMap from '@/views/newsMap/index'
import dayNews from '@/views/dayNews/index'
import Epidemic from '@/views/Epidemic/index'
import Set from '@/views/Set/index'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta:{
        auth:true//添加字段判断该页面是否需要登录
      },
      children:[
        {
          path: '/newsmap',
          name: 'newsMap',
          meta:{
            auth:true//添加字段判断该页面是否需要登录
          },
          component: newsMap
        },
        {
          path: '/daynews',
          name: 'dayNews',
          meta:{
            auth:true//添加字段判断该页面是否需要登录
          },
          component: dayNews
        },
        {
          path: '/epidemic',
          name: 'Epidemic',
          meta:{
            auth:true//添加字段判断该页面是否需要登录
          },
          component: Epidemic          
        },
        {
          path: '/set',
          name: 'Set',
          meta:{
            auth:true//添加字段判断该页面是否需要登录
          },
          component: Set          
        },
      ]
    },
  ]
})
// 路由守卫
router.beforeEach((to,from,next)=>{
  // to要跳转到的路径
  // from从哪个路径来
  // next往下执行的回调
  // 在localStorage中获取token
  let flag = localStorage.getItem('id')
  // 判断该页面是否需要登录
  if(to.meta.auth){
      if(flag){
          next()
      }else{
          // 否则跳转到login登录页面
          next({
              path:'/',
          })
      }
  }else{
      next()
  }
})
export default router
